/**
 *
 * Description: 
 *
 * @author lirong
 * @version 1.0
 * <pre>
 * Modification History: 
 * Date         Author      Version     Description 
 * ------------------------------------------------------------------ 
 * 2012-3-15    lirong       1.0        1.0 Version 
 * </pre>
 */

package com.creditease.mobile.rss.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.*;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.creditease.mobile.R;

/**
 * Description:
 *
 * @author lirong
 * @version 1.0
 *          <p/>
 *          <pre>
 *                   Modification History:
 *                   Date         Author      Version     Description
 *                   ------------------------------------------------------------------
 *                   2012-3-15    lirong       1.0        1.0 Version
 *                   </pre>
 */

public class RssReaderDetail extends Activity {
    private WebView webView;
    private float startX;
    private float endX;
    private TextView titleText;
    private ProgressBar processbar;
    private ProgressBar loadingProcessbar;
    private final String TAG = RssReaderDetail.class.getName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.rss_reader_detail);
//		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
        // 设置Home图标
        ImageView titleView = (ImageView) findViewById(R.id.titleIcon);
        Drawable drawable = getResources().getDrawable(R.drawable.icon_back);
        titleView.setAdjustViewBounds(true);
        titleView.setImageDrawable(drawable);
        titleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RssReaderDetail.this.onBackPressed();
            }
        });
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        processbar = (ProgressBar) findViewById(R.id.progressbar_updown);
        loadingProcessbar=(ProgressBar)findViewById(R.id.rss_loading_bar);
        // webView.setInitialScale(100);
        // 支持缩放
        webView.getSettings().setBuiltInZoomControls(true);
        // webView.getSettings()

        Intent intent = getIntent();
        String url = intent.getExtras().getString("url");
        String title = intent.getExtras().getString("title");
        // 标题
        titleText = (TextView) findViewById(R.id.titleText);
        titleText.setText(title);
        String urlString = "http://gate.baidu.com/tc?from=opentc&src=" + url;

        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        // webView.loadData(buf.toString(), "text/html", "UTF-8");
        webView.loadUrl(urlString);

        //点击后退按钮,让WebView后退一页(也可以覆写Activity的onKeyDown方法)
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {  //表示按返回键时的操作
                        webView.goBack();   //后退

                        //webview.goForward();//前进
                        return true;    //已处理
                    }
                }
                return false;
            }
        });
        webView.setWebViewClient(new RssWebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // super.onProgressChanged(view, newProgress);
                processbar.setProgress(newProgress);
                if (newProgress == 100) {
                    processbar.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 第一次按下事件
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = event.getX();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            endX = event.getX();
            // 从左往右
            if (startX < endX) {

            } else if (startX > endX) {

            }
        }

        return super.onTouchEvent(event);
    }

    private class RssWebViewClient extends WebViewClient {
        // 在点击请求的是链接是才会调用，重写此方法返回true表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边。
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d(TAG, "load url=" + url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            processbar.setVisibility(View.VISIBLE);
            loadingProcessbar.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TextView tv = (TextView) findViewById(R.id.loadingMsg);
            // tv.setVisibility(View.GONE);
            loadingProcessbar.setVisibility(View.INVISIBLE);
            super.onPageFinished(view, url);
        }

        // 在加载页面资源时会调用，每一个资源（比如图片）的加载都会调用一次。
        @Override
        public void onLoadResource(WebView view, String url) {
            // Log.d(TAG, "URL=" + url);
            super.onLoadResource(view, "http://gate.baidu.com/tc?from=opentc&src=" + url);
        }

        @Override
        public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg) {
            super.onTooManyRedirects(view, cancelMsg, continueMsg);
        }

        @Override
        public void onReceivedError(WebView view,
                                    int errorCode,
                                    String description,
                                    String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onFormResubmission(WebView view, Message dontResend, Message resend) {
            super.onFormResubmission(view, dontResend, resend);
        }

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            super.doUpdateVisitedHistory(view, url, isReload);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
        }

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler,
                                              String host,
                                              String realm) {
            super.onReceivedHttpAuthRequest(view, handler, host, realm);
        }

        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            return super.shouldOverrideKeyEvent(view, event);
        }

        @Override
        public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
            super.onUnhandledKeyEvent(view, event);
        }

        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
        }
    }
}
